package com.liuya.safe.controller;

import com.liuya.base.BaseController;
import com.liuya.base.BaseModel;
import com.liuya.common.I18N;
import com.liuya.common.WebUtil;
import com.liuya.safe.tree.AbstractTreeHandler;
import org.springframework.ui.ModelMap;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 树处理基础类
 * @author liuya
 * @date 20160826
 */
public class AbstractTreeContrller extends BaseController{

//    @Autowired
//    @Qualifier("queryTreeHandler")
    protected  AbstractTreeHandler treeHandler;
    // 查询会话
    public String openTree(HttpServletRequest request, ModelMap modelMap){
        Map<String, Object> params = WebUtil.getParameters(request);
        modelMap.addAttribute("treeType", treeHandler.getTreeType());
        modelMap.addAttribute("pageTitle", I18N.getInstance().say(treeHandler.getPageTitle()));
        return treeHandler.getTreePage();
    }

    public Object loadTree(HttpServletRequest request, ModelMap modelMap){
        Map<String, Object> params = WebUtil.getParameters(request);
        return treeHandler.getTree();
    }

    public String menu(HttpServletRequest request, ModelMap modelMap){
        Map<String, Object> params = WebUtil.getParameters(request);
        //获取当前编辑节点的ID
        String id = treeHandler.getId(request);
        Object node = treeHandler.getNodeById(id);
        modelMap.addAttribute("node", node);

        return treeHandler.getContextMenuPage();
    }

    public Object preCreate(HttpServletRequest request, ModelMap modelMap){
        return treeHandler.getCreatePage();
    }

    public Object add(HttpServletRequest request, ModelMap modelMap) {
        Object node = treeHandler.getNode(request);
        boolean result = false;
        result = treeHandler.addNode(node);
        return result;
    }

    public Object preCopy(HttpServletRequest request, ModelMap modelMap){
        String id = treeHandler.getId(request);
        Object node = treeHandler.getNodeById(id);
        modelMap.addAttribute("node", node);
        return treeHandler.getCopyPage();
    }

    public Object copy(HttpServletRequest request, ModelMap modelMap){
        Object node = treeHandler.getNode(request);
        boolean result = treeHandler.copyNode(node);
        return result;
    }

    public Object preModify(HttpServletRequest request, ModelMap modelMap){
        String id = treeHandler.getId(request);
        Object node = treeHandler.getNodeById(id);
        modelMap.addAttribute("node", node);

        return treeHandler.getModifyPage();
    }

    public Object isNameValid(HttpServletRequest request, ModelMap modelMap){
        String name = request.getParameter("name");
        BaseModel node = treeHandler.getNodeByName(name);
        String sId = request.getParameter("id");

        boolean valid = false;
        if (node == null || ("n" + node.getId()).equals(sId)) {
            valid = true;
        }

        return valid + "";
    }

    public Object delete(HttpServletRequest request, ModelMap modelMap){
        String id = treeHandler.getId(request);
        boolean result = treeHandler.deleteNode(id);
        return result;
    }

    public Object update(HttpServletRequest request, ModelMap modelMap){
        Object node = treeHandler.getNode(request);
        return treeHandler.updateNode(node);
    }

    public Object move(HttpServletRequest request, ModelMap modelMap){
        String parentId = treeHandler.getParentId(request);
        String id = treeHandler.getId(request);
        int position = treeHandler.getPosition(request);

        return treeHandler.moveNode(parentId, id, position);
    }
}
